﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SweetSurvey.Models;
using SweetSurvey.Models.Repositories;

namespace SweetSurvey.Tests.RepositoryTests
{
    /// <summary>
    ///This is a test class for SurveyRepositoryTest and is intended
    ///to contain all SurveyRepositoryTest Unit Tests
    ///</summary>
    [TestClass]
    public class SurveyRepositoryTest : RepositoryTestBase
    {
        #region Create Sample Data

        public static string CreateSampleSurvey()
        {
            return (new SurveyRepository()).Create(new Survey()
            {
                Title = "Sample Survey",
                Published = false,
                EditorId = EditorRepositoryTest.CreateSampleEditor(),
                DateCreated = DateTime.Now
            });
        }

        #endregion

        /// <summary>
        ///A test for Update
        ///</summary>
        [TestMethod]
        public void UpdateTest()
        {
            // variables
            var newTitle = "Sample Survey (Updated)";

            // prepare
            var surveyId = CreateSampleSurvey();
            var surveyRepository = new SurveyRepository();
            
            // act
            var survey = surveyRepository.Get(surveyId);
            survey.Title = newTitle;
            surveyRepository.Update(survey);

            // assert
            survey = surveyRepository.Get(surveyId);
            Assert.AreEqual<string>(newTitle, survey.Title);
        }

        /// <summary>
        ///A test for Delete
        ///</summary>
        [TestMethod]
        public void DeleteTest()
        {
            // variables
            var expectedMessage = "Sequence contains no elements";

            // prepare
            string surveyId = CreateSampleSurvey();
            var surveyRepository = new SurveyRepository();

            // act
            surveyRepository.Delete(surveyId);

            // assert
            try { surveyRepository.Get(surveyId); Assert.Fail(); }
            catch (InvalidOperationException exception) { Assert.AreEqual<string>(expectedMessage, exception.Message); }
            catch (Exception) { Assert.Fail(); }
        }
    }
}
